{% extends "pos/base.html" %}
{% load static %}
<!-- Page title  -->
{% block title %}Sales{% endblock title %}

<!-- Specific Page CSS goes HERE  -->
{% block stylesheets %}
<!-- Datatables -->
<link href="{% static 'vendor/datatables/dataTables.bootstrap4.min.css ' %}" rel="stylesheet">
<link href="{% static 'css/pagination_buttons.css ' %}" rel="stylesheet">
{% endblock stylesheets %}

<!-- Page Heading -->
{% block heading %}Sales{% endblock heading %}

<!-- Page content  -->
{% block content %}

    <!--Create new sale-->
    <div class="row ml-0 mb-3">
        <a href="{% url 'sales:sales_add' %}">
            <button type="button" class="btn btn-success font-weight-bold">
                    <i class="fas fa-plus mr-2"></i>
                    Create new sale
            </button>
        </a>
    </div>

    <!-- DataTable -->
    <div class="card shadow mb-12">
        <div class="card-header py-3">
            <h6 class="m-0 font-weight-bold text-primary">Customers</h6>
        </div>
        <div class="card-body">
            <div class="table-responsive">
                <table class="table table-bordered table-hover" id="dataTable" width="100%" cellspacing="0">
                    <thead>
                        <tr>
                            <th style="width:5%">#</th>
                            <th style="width:15%">ID</th>
                            <th style="width:25%">Date</th>
                            <th style="width:20%">Customer</th>
                            <th class="text-center" style="width:10%">Total</th>
                            <th class="text-center" style="width:5%">Items</th>
                            <th class="text-center" style="width:10%">Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for s in sales %}
                        <tr>
                            <td>{{forloop.counter}}</td>
                            <td>{{s.id}}</td>
                            <td>{{s.date_added}}</td>
                            <td>{{s.customer.get_full_name}}</td>
                            <td class="text-right" >{{s.grand_total}}</td>
                            <td class="text-center" >{{s.sum_items}}</td>
                            <td class="text-center">
                                <!--View-->
                                <a href="{% url 'sales:sales_details' s.id %}" class="text-decoration-none">
                                    <button type="button" class="btn btn-info btn-sm" data-bs-toggle="tooltip" title="Update sale">
                                        <i class="fas fa-eye"></i>
                                    </button>
                                </a>
                                <!--Print receipt-->
                                <a href="{% url 'sales:sales_receipt_pdf' s.id %}" class="text-decoration-none">
                                    <button type="button" class="btn btn-dark btn-sm" data-bs-toggle="tooltip" title="View Receipt">
                                        <i class="fas fa-receipt"></i>
                                    </button>
                                </a>
                            </td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
        </div>
    </div>
{% endblock content %}

<!-- Specific Page JS goes HERE  -->
{% block javascripts %}
<!-- Datatables -->
<script src="{% static 'vendor/datatables/jquery.dataTables.min.js' %}"></script>
<script src="{% static 'vendor/datatables/dataTables.bootstrap4.min.js' %}"></script>
<!--Datatable buttons-->
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jszip/2.5.0/jszip.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.36/pdfmake.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.36/vfs_fonts.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/1.13.1/js/jquery.dataTables.min.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/buttons/2.3.3/js/dataTables.buttons.min.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/buttons/2.3.3/js/buttons.colVis.min.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/buttons/2.3.3/js/buttons.html5.min.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/buttons/2.3.3/js/buttons.print.min.js"></script>
<!--Moment JS-->
<script type="text/javascript" src="https://cdn.jsdelivr.net/momentjs/latest/moment.min.js"></script>

<!--Datatables-->
<script>
    // Call the dataTables jQuery plugin
    $(document).ready(function() {
        tblCategories = $('#dataTable').DataTable({
            dom: 'Bfrtip', // Buttons are draw at this element
            buttons: [
                {
                extend: 'print',
                text: 'Print <i class="fas fa-file ml-2"></i>',
                titleAttr: 'Print',
                className: 'btn btn-info',
                exportOptions: {
                    columns: [ 0, 1, 2, 3 ,4, 5]
                }            
                },
                {
                    extend: 'excelHtml5',
                    text: 'Download Excel <i class="fas fa-file-excel ml-2"></i>',
                    titleAttr: 'Download Excel',
                    className: 'btn btn-success ml-2',
                    exportOptions: {
                        columns: [ 0, 1, 2, 3 ,4, 5 ]
                    } 
                },
                {
                    extend: 'pdfHtml5',
                    text: 'Download PDF <i class="fas fa-file-pdf ml-2"></i>',
                    titleAttr: 'Download PDF',
                    className: 'btn btn-danger ml-2',
                    download: 'open',
                    orientation: 'vertical',
                    pageSize: 'A4',
                    title: '*',
                    exportOptions: {
                        columns: [ 0, 1, 2, 3 ,4 ,5]
                    },
                    customize: function (doc) {
                        doc.styles = {
                            header: {
                                fontSize: 18,
                                bold: true,
                                alignment: 'center'
                            },
                            subheader: {
                                fontSize: 13,
                                bold: true
                            },
                            quote: {
                                italics: true
                            },
                            small: {
                                fontSize: 8
                            },
                            tableHeader: {
                                bold: true,
                                fontSize: 11,
                                color: 'white',
                                fillColor: '#2d4154',
                                alignment: 'center'
                            }
                        };
                        doc.content[1].table.widths = ['10%', '10%', '30%', '20%', '20%', '10%'];
                        doc.content[1].margin = [0, 35, 0, 0];
                        doc.content[1].layout = {};
                        doc['footer'] = (function (page, pages) {
                            return {
                                columns: [
                                    {
                                        alignment: 'left',
                                        text: ['Creation date: ', {text: moment().format('MM-DD-YYYY (HH:mm:ss)')}]
                                    },
                                    {
                                        alignment: 'right',
                                        text: ['page ', {text: page.toString()}, ' de ', {text: pages.toString()}]
                                    }
                                ],
                                margin: 20
                            }
                        });

                    }
                }
            ],
            deferRender: true,
            columnDefs: [
                {
                    targets: [-1], // column index (start from 0)
                    orderable: false, // set orderable false for selected columns
                }
            ],
        });
    });
</script>
{% endblock javascripts %}